<?php
/**
 * QQAS - share your knowledge to the world!
 *
 * @author		xiaochong0302 <xiaochong0302@gmail.com>
 * @copyright	Copyright (c) 2011, Koogua Studio.
 * @license		http://www.qqas.org/user_guide/license.html
 * @link		http://www.qqas.org
 * @since		Version 1.0
 * @filesource
 */


class TagAction extends HomeAction
{

    protected $Tag;

	function _initialize()
	{
		parent::_initialize();
		$this->Tag = D('Tag');
	}


    //带标签的问题
    public function index()
    {
    	
    	import('ORG.Util.Page');
		
    	$View = D('QuestionTagUserView');
		
        $tagID = isset($_GET['tid']) ? intval($_GET['tid']) : 0;
    	$tag = $this->Tag->find($tagID);
        if (empty($tag))
        {
        	$this->error('tag_not_existed');
        }

        $map = $order = array();
		$parameter = '';

		L('tagged_questions', sprintf(L('tagged_questions'), $tag['name']));
		$seo = array(
    		'title' => L('tagged_questions'),
    		'keywords' => $tag['name'],
    		'description' => $tag['description'],
    	);

		$status = isset($_GET['status']) ? strtolower($_GET['status']) : 'all';
		switch ($status)
		{
		    case 'all':
		        break;
			case 'unanswered':
				$map['status'] = 1;
				$map['Question.answer_count'] = 0;
				break;
			case 'open':
			    $map['status'] = 1;
			    break;
			case 'voting':
			    $map['status'] = 2;
			    break;
			case 'resolved':
			    $map['status'] = 3;
			    break;
			case 'reward':
			    $map['is_reward'] = 1;
			    break;
			case 'urgent':
			    $map['is_urgent'] = 1;
			    break;
		}

		$parameter .= '&status='.$status;

		$map['tag_id'] = $tagID;
		$map['is_visible'] = 1;
		$parameter .= '&tid='.$tagID;

		$order['question_id'] = 'desc';

    	$pageSize = $this->_global['questions_per_page'];
		$maxPage  = $this->_global['max_question_pages'];
		if ($maxPage && isset($_GET[C('VAR_PAGE')]) && intval($_GET[C('VAR_PAGE')]) > $maxPage) {
    		$_GET[C('VAR_PAGE')] = $maxPage;
    	}

		

    	$total = $View->where($map)->count();
    	$Page  = new Page($total, $pageSize);
		$Page->parameter = $parameter;
		$pagination = $Page->show();
    	$questions  = $View->where($map)->order($order)->limit($Page->firstRow.','.$Page->listRows)->select();

    	$this->assign('seo', $seo);
    	$this->assign('tag', $tag);
    	$this->assign('status', $status);
    	$this->assign('questions', $questions);
    	$this->assign('pagination', $pagination);

    	$this->display();
    }


    //tag列表
	public function listing()
    {
		$seo = array(
    		'title' => L('tags'),
    		'keywords' => '',
    		'description' => '',
    	);

    	$parameter = '';
    	$map = $order = array();
    	$sort = isset($_GET['sort']) ? $_GET['sort'] : 'popular';

    	switch ($sort)
    	{
    		case 'name':
    			$order['name'] = 'asc';
    			break;
    		case 'popular':
    			$order['question_count'] = 'desc';
    			break;
    		case 'time':
    		    $order['create_time'] = 'asc';
    	}

    	$map['status'] = 1;
    	$parameter .= '&sort='.$sort;
    	$pageSize = 50;

		import('ORG.Util.Page');

    	$total = $this->Tag->where($map)->count();
    	$Page  = new Page($total, $pageSize);
		$Page->parameter = $parameter;
		$pagination = $Page->show();
    	$tags = $this->Tag->where($map)->order($order)->limit($Page->firstRow.','.$Page->listRows)->select();

    	$this->assign('seo', $seo);
    	$this->assign('sort', $sort);
		$this->assign('tags', $tags);
    	$this->assign('pagination', $pagination);
    	$this->display();
    }

}


/* End of file TagAction.class.php */
/* Location: ./App/Lib/Action/Home/TagAction.class.php */